package com.xxx.yuedong.config;

import com.xxx.yuedong.common.consts.QueueConstants;
import com.xxx.yuedong.rabbitmq.MyAckReceiver;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author HG
 * @version 1.0
 **/

/**
 * 消息接收确认
 */
//@Configuration
public class MessageListenerConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Autowired
    private MyAckReceiver myAckReceiver;

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setConcurrentConsumers(1);
        container.setMaxConcurrentConsumers(1);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);// Rabbitmq默认是自动确认，这里改为手动

        //设置监听队列
        container.setQueueNames(QueueConstants.COMMENT, QueueConstants.FOLLOW,
                                QueueConstants.LIKE, QueueConstants.UPDATE);
        /*设置多个如下，前提是队列必须已存在
        container.setQueueNames("TestDirectQueue", "TestDirectQueue2", "TestDirectQueue3");

        另一种设置队列的方法，如果使用这种情况，那么要设置多个就使用addQueues
        container.setQueues(new Queue("TestDirectQueue", true));
        多个:
        container.addQueues(new Queue("TestDirectQueue1", true));
        container.addQueues(new Queue("TestDirectQueue2", true));*/
        container.setMessageListener(myAckReceiver);

        return container;
    }
}
